#include <fstream>
#include "global_const.h"
#include "student.h"
#include "teacher.h"
#include "administrator.h"
#include "lab_reservation.h"

// 获取用户输入
vector<string> get_input()
{
    string input = "";
    cin >> input;

    vector<string> tokens;
    string token;
    istringstream ss(input);
    while (getline(ss, token, ','))
    {
        tokens.push_back(token);
    }
    return tokens;
}

// 登录功能定义
Identity *login(int type, string filename)
{
    switch (type)
    {
    case 1: // student
    {
        cout << STUDENT_TIPS << endl;
        // 获取用户输入
        vector<string> input = get_input();

        ifstream ifs(filename, ios::in);
        if (!ifs.is_open())
        {
            cout << "登录失败" << endl;
            return NULL;
        }

        // 校验信息
        int id;
        string name;
        string pwd;
        while (ifs >> id && ifs >> name && ifs >> pwd)
        {
            if (atoi(input[0].data()) == id && input[1] == name && input[2] == pwd)
            {
                cout << "欢迎 " << name << endl;
                return new Student(id, name, pwd);
            }
        }
        break;
    }
    case 2: // teacher
    {
        cout << TEACHER_TIPS << endl;
        // 获取用户输入
        vector<string> input = get_input();

        ifstream ifs(filename, ios::in);
        if (!ifs.is_open())
        {
            cout << "登录失败" << endl;
            return NULL;
        }

        // 校验信息
        int id;
        string name;
        string pwd;
        while (ifs >> id && ifs >> name && ifs >> pwd)
        {
            if (atoi(input[0].data()) == id && input[1] == name && input[2] == pwd)
            {
                cout << "欢迎 " << name << endl;
                return new Teacher(id, name, pwd);
            }
        }
        break;
    }
    case 3: // administrator
    {
        cout << ADMIN_TIPS << endl;
        // 获取用户输入
        vector<string> input = get_input();

        /*
        // 从admin.txt文件校验
        ifstream ifs(filename, ios::in);
        if (!ifs.is_open())
        {
            cout << "登录失败" << endl;
            return NULL;
        }

        string name;
        string pwd;
        while (ifs >> name && ifs >> pwd)
        {
            if (input[0] == name && input[1] == pwd)
            {
                cout << "欢迎 " << name << endl;
                return new Administrator(name, pwd);
            }
        }
        */
        // 默认用户
        if (input[0] == Administrator::m_def_account && input[1] == Administrator::m_def_pwd)
        {
            cout << "欢迎 " << input[0] << endl;
            return new Administrator(input[0], input[1]);
        }

        break;
    }
    break;
    default:
        cout << "身份信息错误" << endl;
        return NULL;
    }

    cout << "登录失败" << endl;
    return NULL;
}

// 注销
void logout(const Identity *i)
{
    delete i;
    exit(0);
    cout << "已注销" << endl;
}

void manage(Identity *&indentity)
{

    int choice;
    while (true)
    {
        indentity->menu();
        cin >> choice;
        if (choice == 0)
        {
            delete indentity;
            cout << "已注销" << endl;
            return;
        }
        indentity->action(choice);
    }
}